package com.bjpowernode.pmes.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.stereotype.Service;

import com.bjpowernode.pmes.bean.AuthHistory;
import com.bjpowernode.pmes.bean.BugTicket;
import com.bjpowernode.pmes.bean.BugTickets;
import com.bjpowernode.pmes.bean.Page;
import com.bjpowernode.pmes.bean.User;
import com.bjpowernode.pmes.dao.AuthHistoryDao;
import com.bjpowernode.pmes.dao.BugTicketDao;
import com.bjpowernode.pmes.service.BugTicketService;

@Service("bugTicketService")
public class BugTicketServiceImpl implements BugTicketService {

	@Resource
	private BugTicketDao bugTicketDao;
	@Resource
	private RepositoryService repositoryService;
	@Resource
	private RuntimeService runtimeService;
	@Resource
	private AuthHistoryDao authHistoryDao;

	public void insertBugTicket(BugTicket bt) {
		bugTicketDao.insertBugTicket(bt);
	}

	public Page<BugTicket> getAllBts(User loginUser, Integer pageno, Integer pagesize) {
		Page<BugTicket> page = new Page<BugTicket>();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("startIndex", (pageno-1)*pagesize);
		paramMap.put("pagesize", pagesize);
		paramMap.put("user", loginUser);
		
		// 查询集合
		List<BugTicket> userList = bugTicketDao.getAllBtList(paramMap);
		page.setDataList(userList);
		
		// 查询数量
		Integer count = bugTicketDao.getBtsCount(paramMap);
		page.setTotalsize(count);
		
		if ( count % pagesize == 0 ) {
			page.setTotalno(count / pagesize);
		} else {
			page.setTotalno(count / pagesize+1);
		}
		
		return page;
	}

	public void updateBt(BugTicket bt) {
		bugTicketDao.updateBt(bt);
	}

	public void deleteBts(BugTickets bts) {
		bugTicketDao.deleteBts(bts);
	}

	public void updateBtStatus(BugTicket bt) {
		// 查询故障单审批流程定义
		ProcessDefinition pd =
			repositoryService
			    .createProcessDefinitionQuery()
			    .processDefinitionKey("bugflow")
			    .latestVersion()
			    .singleResult();
		
		// 启动流程
		ProcessInstance pi = runtimeService.startProcessInstanceById(pd.getId());

		bt.setPiid(pi.getId());
		
		bugTicketDao.updateBtStatus(bt);
	}

	public BugTicket getBtByPI(String piid) {
		return bugTicketDao.getBtByPI(piid);
	}

	public void insertAuthHistory(AuthHistory ah) {
		authHistoryDao.insertAuthHistory(ah);
	}

	public void updateBtAuthStatus(BugTicket bt) {
		bugTicketDao.updateBtAuthStatus(bt);
	}

	public List<AuthHistory> getBtHistorys(BugTicket bt) {
		return authHistoryDao.getBtHistorys(bt);
	}

	public Integer getBtHistoryCnt(BugTicket bt) {
		return authHistoryDao.getBtHistoryCnt(bt);
	}

	public Page<BugTicket> getPageByAuthed(User loginUser, Integer pageno,
			Integer pagesize) {
		Page<BugTicket> btPage = new Page<BugTicket>();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("userid", loginUser.getId());
		paramMap.put("startIndex", (pageno-1)*pagesize);
		paramMap.put("pagesize", pagesize);
		
		List<BugTicket> btList = bugTicketDao.getBtsByHistory(paramMap);
		btPage.setDataList(btList);
		Integer cnt = bugTicketDao.getBtsCntByHistory(paramMap);
		btPage.setTotalsize(cnt);
		
		return btPage;
	}

	public Page<BugTicket> getPageByFinished(Integer pageno, Integer pagesize) {
		Page<BugTicket> btPage = new Page<BugTicket>();
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("startIndex", (pageno-1)*pagesize);
		paramMap.put("pagesize", pagesize);
		
		List<BugTicket> btList = bugTicketDao.getBtsForFinished(paramMap);
		btPage.setDataList(btList);
		Integer cnt = bugTicketDao.getBtsCntForFinished(paramMap);
		btPage.setTotalsize(cnt);
		
		return btPage;
	}
}
